/**
 * 日常任务
 * @author truexin
 * @date   2016/07/05
 */
(function (lf, r) {
    
    var ajax = lf.utils.ajax,
        doAction = lf.bridgeJs.doAction,
        RC = r.createClass;
    
    var menuList = [
        {
            code: "TASK-ADD",
            type: 'btn',
            name: ' 添加任务',
        },
        {
            code: "TASK-CONFIRM",
            type: 'text',
            name: '任务确认',
        },
        {
            code: "TASK-FEEDBACK",
            type: 'text',
            name: '任务反馈',
        },
        {
            code: "TASK-CHECK",
            type: 'text',
            name: '任务审核',
        },
        // {
        //     code: "TASK-BACK",
        //     type: 'text',
        //     name: '任务撤回',
        // },
        // {
        //     code: "TASK-HELP",
        //     type: 'text',
        //     name: '协办任务',
        // },
        {
            code: "TASK-DISPATCH",
            type: 'numHide',
            name: '我派遣的',
        },
        {
            code: "TASK-COMPLETE",
            type: 'numHide',
            name: '已完成',
        },
    
    ]
    
    const CONFIRM = 'confirm',
        FEEDBACK = 'feedback',
        CHECK = 'check',
        BACK = 'back',
        HELP = 'help',
        COMPLETE = 'complete',
        ADD = 'add',
        DISPATCH = 'dispatch';
    
    const ALL = 'all';
    
    var Panel = React.createClass({
        getInitialState: function () {
            return ({
                numList: [],
            })
        },
        componentWillMount: function () {
            var user = this.props.user,
                me = this;
            me.resetNum(user);
            lf.appJs.refresh(function (user) {
                me.resetNum(user);
            });
        },
        resetNum: function (user) {
            var me = this;
            //获取数字
            ajax({
                type: 'post',
                data: {
                    staffId: user.id,
                },
                success: function (data) {
                    me.setState({
                        numList: data.data,
                    });
                }
            }, lf.api.worktable.taskNum);
        },
        componentDidMount: function () {
            let $listPage = $('.list');
            var me = this;
            $listPage.each(function (i) {
                var $me = $(this);
                $me.off();
                $me.on('tap', function () {
                    var code = me.props.menuList[i].code;
                    
                    if (code == 'TASK-CONFIRM')
                        doAction('daily-work-list.html?type=' + CONFIRM, '任务确认');
                    
                    if (code == 'TASK-FEEDBACK')
                        doAction('daily-work-list.html?type=' + FEEDBACK, '任务反馈');
                    
                    if (code == 'TASK-CHECK')
                        doAction('daily-work-list.html?type=' + CHECK, '任务审核');
                    
                    if (code == 'TASK-BACK')
                        doAction('daily-work-list.html?type=' + BACK, '任务撤回');
                    
                    if (code == 'TASK-HELP')
                        doAction('daily-work-list.html?type=' + HELP, '协办任务');
                    
                    if (code == 'TASK-DISPATCH')
                        doAction('daily-work-list.html?type=' + DISPATCH, '任务派遣');
                    
                    if (code == 'TASK-COMPLETE')
                        doAction('daily-work-list.html?type=' + COMPLETE + '&flag=' + ALL, '已完成');
                    
                    if (code == 'TASK-ADD')
                        doAction('daily-work-add.html?type=' + ADD, '添加');
                    
                });
            })
        },
        render: function () {
            let item2DArray = lf.utils.to2DArray(this.props.menuList || [], 2);
            let menus = item2DArray.map((array, i) => {
                return (
                    <div key={'array' + i} className="item-row">
                        {
                            array.map(menu => {
                                return <div key={menu.id} className="item list">
                                    <div>
                                        <span
                                            className={"redNum " +
                                            (menu.type == 'btn' ?
                                            'mui-icon mui-icon-plusempty '+ menu.type :
                                            menu.type)}>
                                            {this.getNum(menu.code)}
                                        </span>
                                        <p className={menu.type}>{menu.name}</p>
                                    </div>
                                </div>
                            })
                        }
                    </div>
                );
            });
            
            return (
                <div className="box">
                    {menus}
                </div>
            );
        },
        getNum(code) {
            let numList = this.state.numList,//this a object
                num = ' ';
            
            
            if (code == 'TASK-CONFIRM')
                num = numList.waitComfirmCount;
            
            if (code == 'TASK-FEEDBACK')
                num = numList.waitFeedbackCount;
            
            if (code == 'TASK-CHECK')
                num = numList.waitCheckCount;
            
            if (code == 'TASK-ADD') {
                num = '';
            }
            
            // if (code == 'TASK-DISPATCH')
            //     num = numList.waitCheckCount;
            
            // if (code == 'TASK-BACK')
            //     num = numList.waitCheckCount;
            //
            // // if (code == 'TASK-COMPLETE')
            // //     num = numList.waitCheckCount;
            //
            // if (code == 'TASK-HELP')
            //     num = numList.waitCheckCount;
            
            return num;
        }
    });
    
    /**
     * props:
     *  user
     *  menuList
     */
    var Body = RC({
        getInitialState: function () {
            return ({
                list: []
            })
        },
        componentWillMount: function () {
            this.getList(this.props.user);
            
        },
        getList: function (user) {
            this.setState({
                list: <Panel user={user} menuList={this.props.menuList}/>
            });
        },
        render: function () {
            //TODO 组件化
            if (!this.props.menuList.length) //数据为空
                return <div style={{textAlign: 'center', paddingTop: '1rem'}}>请联系管理员授权</div>
            
            return (
                <div className="notice">
                    {this.state.list}
                </div>
            );
        }
    });
    
    lf.appJs.getUser(function (user) {
        /*  ajax({
         type: 'POST',
         data: {
         staffId: user.id,
         sysMenuId: lf.utils.getUrlParam('menuId')
         }
         }, lf.api.menuList, function (menuList) {*/
        r.render(
            <Body user={user} menuList={menuList}/>,
            document.getElementById('root'), function () {
                $('#root').css('background', '#f4f4f4');
                
            }
        )
        // })
    });
    
})(liefeng, React)
